package com.ktmall.ktmalluijava.service.impl;

import com.ktmall.ktmalluijava.mapper.AttrValuesMapper;
import com.ktmall.ktmalluijava.pojo.AttrValues;
import com.ktmall.ktmalluijava.service.AttrValuesService;
import com.ktmall.ktmalluijava.utils.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/16 上午8:48
 */
@Service
public class AttrValuesServiceImpl implements AttrValuesService  {

    @Resource
    private AttrValuesMapper attrValuesMapper;
    /**
     * 获取分页对象
     *
     * @param attrId
     * @param name
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<AttrValues> getPage(Integer attrId, String name, Integer pageNo, Integer pageSize) {
        Page<AttrValues> page = new Page<>();
        page.setPageSize(pageSize);
        page.setTotalCount(attrValuesMapper.count(attrId,name));
        page.setPageNo(pageNo);
        int offset = (page.getPageNo()-1)*page.getPageSize();
        page.setData(attrValuesMapper.select(attrId,name,offset,page.getPageSize()));
        return page;
    }

    /**
     * 添加
     *
     * @param attrValues
     * @return
     */
    @Override
    public int add(AttrValues attrValues) {
        return attrValuesMapper.add(attrValues);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public AttrValues detail(Integer id) {
        return attrValuesMapper.detail(id);
    }

    /**
     * 获取总记录数
     *
     * @param attrId
     * @param name
     * @return
     */
    @Override
    public int count(Integer attrId, String name) {
        return attrValuesMapper.count(attrId,name);
    }

    /**
     * 修改
     *
     * @param attrValues
     * @return
     */
    @Override
    public int update(AttrValues attrValues) {
        return attrValuesMapper.update(attrValues);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Integer... id) {
        if(id.length==1){
            return attrValuesMapper.delete(id[0]);
        }
        int count = 0;
        for (Integer integer : id) {
            count += attrValuesMapper.delete(integer);
        }
        return count;
    }

    /**
     * 获取所有的规格属性值
     *
     * @return
     */
    @Override
    public List<AttrValues> getAll() {
        return attrValuesMapper.getAll();
    }
}
